$(function () {
  function setBasicInfo(data) {
      var status;
      if(data.status==0)
          status=' <span class="label label-warning">'+gettext('Unconducted')+'</span>';
      else if (data.status==1)
          status=' <span class="label label-success">'+gettext('Running')+'</span>';
      else if(data.status==2)
          status=' <span class="label label-primary">'+gettext('Completed')+'</span>';
      else
          status=' <span class="label label-danger">'+gettext('Failure')+'</span>';
    $('#t-title').text(data.title || '— —');
    // var add_time=time.strftime("%b %d %Y %H:%M:%S", data.pub_data);
    var times=data.pub_data.split('.')[0].split('T');
    var pub_time=times[0]+' '+times[1];
    //wanjian--version
    // var d = new Date(data.pub_data);
    // var local_data = d.toLocaleString('en-GB');
    $('#t-time').text(pub_time || '— —');
    $('#t-author').text(data.author || '— —');
    $('#t-status').html(status);
  }

    function setContentInfo(data) {
        $('#t-dataset').text(data.dataset || '— —');
        $('#t-algorithm').text(data.algorithm || '— —');
        $('#t-label').text(data.label || '— —');
        $('#t-features').text(data.features || '— —');
        //params
        var file_id=data.id;
        $("#result_file").click(function () {
            export_dataset(file_id);
        });
        var params = data.params;
        if (params !== null) {
            var params_keys = Object.keys(params);
            var params_opt = '<ul>';
            params_keys.forEach(function (it) {
                params_opt += '<li>'+it + ' : ' + params[it] + '</li>';
            });
            params_opt += '</ul>';
            $('#t-params').html(params_opt);
        }
        else{
            $('#t-params').text('— —');
        }
        //result
        var result = data.result;
        var reuslt_keys = '';
        var result_opt = '<textarea style="width:80%" rows="10">';
        if (result !== null) {
            reuslt_keys = Object.keys(result);
            $.each(reuslt_keys,function (index,it) {
                console.log(it);
                if(it==='filename'){

                }
                else{
                    if(it==='centers'){
                        var arrays=result[it];
                        result_opt+='▪ '+it + ' : ';
                        arrays.forEach(function(i){
                           console.log(i);
                           result_opt+=i+'\n';
                        });
                     }
                    else{
                        result_opt +='▪ '+it + ' : ' + result[it]+'\n' ;
                    }
                }
            });
            result_opt +='</textarea>';
            $('#t-result').html(result_opt);
        }
        else{
            $('#t-result').text('— —');
        }
        //visual
        var visual_opt = '';
        if (data.visual_pic !== null) {
            data.visual_pic.forEach(function (it) {
                // console.log(it);
                visual_opt += '<img src="/media/' + it + '" alt="result"></<br>';
            });
            $('#t-visual').append(visual_opt);
        }
        else{
            $('#t-visual').text('— —');
        }
    }

    $.requestJSON({
        method: 'GET',
        url: Urls.resolve('mlplatform_api_v1:task_detail', {pk: window.location.href.split('/').pop()}),
        onSuccess: function (json) {
            // console.log(json);
            setBasicInfo(json.data);
            setContentInfo(json.data);
        },
        onError: function () {
            window._jqHandled = true;
        }
    });
});


function export_dataset(did) {
  window.location.href = Urls.resolve('mlplatform_api_v1:result_download', {pk: did});
}